package com.chexiehui.jc.mvp.home;

import com.chexiehui.jc.Constants;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePage;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.entity.CarInfo;
import com.chexiehui.jc.entity.PostInfo;

/**
 * @Description 首页新车
 * @Auth SongJun
 * @Date 2019/11/20 10:05
 **/
public class HomeSlideNewCarPresenter extends BasePresenter<HomeSlideNewCarCovenant.View, HomeSlideNewCarCovenant.Stores>
        implements HomeSlideNewCarCovenant.Presenter {

    public HomeSlideNewCarPresenter(HomeSlideNewCarCovenant.View view) {
        attachView(view);
    }

    @Override
    public void getCarList() {
        addSubscription(appStores.getCarList(1, 10),
                new ApiCallback<BaseModel<BasePage<CarInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<BasePage<CarInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().getItems() != null &&
                                    model.getData().getItems().size() > 0) {
                                mvpView.onGetCarListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "没有找到任何数据");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetCarListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void getPostList(int pageNum) {
        addSubscription(appStores.getPostList(pageNum, 10),
                new ApiCallback<BaseModel<BasePage<PostInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<BasePage<PostInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().getItems() != null &&
                                    model.getData().getItems().size() > 0) {
                                mvpView.onGetPostListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "没有找到任何数据");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetPostListFailure(new BaseModel<>(code, msg));
                    }
                });
    }
}